package com.example.asus.yuejian_10.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.base.BaseFragment;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

/**
 */
public class LoginFragment extends BaseFragment {

    private EditText username;
    private EditText password;

    private String userName;
    private String userPass;

    private Button btn_reg;
    private Button btn_login;

    private Retrofit retrofit;
    //private RetrofiService service;

    //声明SharedPreferences对象。
    private SharedPreferences sharedPreferences;
    //声明Editor对象。
    private SharedPreferences.Editor editor;


    public LoginFragment() {
        // Required empty public constructor
        retrofit =
                new Retrofit.Builder()
                        .baseUrl(Config.URL)
                        .addConverterFactory(GsonConverterFactory.create()).build();
        //service = retrofit.create(RetrofiService.class);
    }


    @Override
    public  View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_login, container, false);
     /*   username = (EditText) view.findViewById(R.id.username);
        password = (EditText) view.findViewById(R.id.password);
        btn_login = (Button) view.findViewById(R.id.btn_login);
        btn_reg = (Button) view.findViewById(R.id.btn_reg);*/

        sharedPreferences=this.getActivity().getSharedPreferences("UserMessage", Context.MODE_PRIVATE);
        editor=sharedPreferences.edit();


        //登录按钮
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Login();
            }
        });

        //注册按钮
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               /* Intent intent =new Intent(getActivity(), RegActivity.class);
                startActivity(intent);*/
                //2 . 启动带有返回结果的Activity
                //Intent intent = new Intent( getActivity(),RegActivity.class);
                //startActivityForResult(intent,1);//requestCode = 1
            }
        });


        return view;
    }

    private void Login() {
        userName = username.getText().toString();
        userPass = password.getText().toString();

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode){

            case 1 : //启动的注册Activity
                /*switch (requestCode){*/
            {
                if (resultCode == RESULT_OK) {

                    String userName = data.getStringExtra("username");
                    String userPass = data.getStringExtra("password");
                    username.setText(userName);
                    password.setText(userPass);
                }

                else if (resultCode == RESULT_CANCELED) {

                }

            }
            break;

            default:

                break;

        }
    }



}



